var loginUsername = "${loginUsername}"; // 获得当前登录人员的userName
mLoginUsername = loginUsername;
var partUid = '${request.getParameter("partUid")}';
var ioType = '${request.getParameter("ioType")}';
mIoType = ioType;
var date = new Date();


//@Bind @dtIOEmp.onDataChange
!function(self,arg,dsIOEmp,dsInvIO) {
	var ioEmp = dsIOEmp.getData("#");
	//alert(ioEmp.get("cardId"));
	dsIOEmp.set("parameter", {
		cardId : ioEmp.get("cardId")
	}).flush();
	
	var ioEmp = dsIOEmp.getData("#");
	if (ioEmp){
		var invio = dsInvIO.insert();
		if(ioType==19){
			invio.set("ioType", 19);
			invio.set("ioTypeDesc", '一般入库');
		}else if (ioType==17){
			invio.set("ioType", 17);
			invio.set("ioTypeDesc", '盘盈入库');
		}else if (ioType==103){
			invio.set("ioType", 103);
			invio.set("ioTypeDesc", '采购入库');
		}else if (ioType==16){
			invio.set("ioType", 16);
			invio.set("ioTypeDesc", '检验入库');
		}else if (ioType==14){
			invio.set("ioType", 14);
			invio.set("ioTypeDesc", '领用入库');
		}
		invio.set("ioState", 1);
		invio.set("ioStateDesc", '草稿');
		invio.set("createTime", date);
		invio.set("ioDate", date);
		
		invio.set("deptUid", ioEmp.get("deptUid"));
		invio.set("dept.deptName", ioEmp.get("dept.deptName"));
		invio.set("ioEmployeeUid", ioEmp.get("employeeUid"));
		invio.set("ioEmployee.employeeName", ioEmp.get("employeeName"));
		//库管员
		invio.set("warehouseManagerUid", ioEmp.get("employeeUid"));
		
		invio.set("warehouseUid",mWarehouseId);
		invio.set("creatorUid", loginUser.get("employeeUid"));
		invio.set("creator.employeeName", loginUser.get("employeeName"));

		var actionGetInvInBillNumber = view.id("actionGetInvInBillNumber");
		actionGetInvInBillNumber.execute(function(result){
			invio.set("invIOId", result);
		});
	}else {
		dorado.MessageBox.alert("请输入正确的编号...");
		dsInvIO.clear();
		dsIOEmp.insert();
	}
	
}

//@Bind #view.onReady
!function(self,arg) {
	var uuid = '${request.getParameter("uuid")}';
	loginUser = view.id("dsUser").getData("#");

	if(uuid){
		var dsInvIO = view.id("dsInvIO");
		
		dsInvIO.set("parameter",{"uuid":uuid});
		
		dsInvIO.flush();
		
		var dsInvIODetail = view.id("dsInvIODetail");
		
		dsInvIODetail.set("parameter",{"invIOUid":uuid});

		dsInvIODetail.flush();
	}
	
	var ctrlTab = view.id("ctrlTabIn");
	if(ioType==19){
		ctrlTab.set("caption", '一般入库');
	}else if (ioType==17){
		ctrlTab.set("caption", '盘盈入库');
	}else if (ioType==103){
		ctrlTab.set("caption", '采购入库');
	}else if (ioType==16){
		ctrlTab.set("caption", '检验入库');
	}else if (ioType==14){
		ctrlTab.set("caption", '领用入库');
	}
	
}



//@Bind #deptTree.onDataRowClick
!function(self, arg, dsInvIO) {
	var invio = dsInvIO.getData("#");
	var curEntity = self.get("currentEntity");
	
	if (curEntity.dataType.get("name") == "dtEmployee") {
		invio.set("deptUid", curEntity.get("deptUid"));
		invio.set("dept.deptName", curEntity.get("dept").get("deptName"));
		invio.set("ioEmployeeUid", curEntity.get("employeeUid"));
		invio.set("ioEmployee.employeeName", curEntity.get("employeeName"));
		
		view.id('formEmployeeName').refreshData();
		view.id('formDeptName').refreshData();
	}
	dorado.widget.DropDown.findDropDown(self).close();
	
}

//@Bind #btnAddInvIo.onClick
!function(self,arg,dsInvIO,dsInvIODetail) {
	dsInvIO.clear();
	dsInvIODetail.clear();
	var invio = dsInvIO.insert();
	if(ioType==19){
		invio.set("ioType", 19);
		invio.set("ioTypeDesc", '一般入库');
	}else if (ioType==17){
		invio.set("ioType", 17);
		invio.set("ioTypeDesc", '盘盈入库');
	}else if (ioType==103){
		invio.set("ioType", 103);
		invio.set("ioTypeDesc", '采购入库');
	}else if (ioType==16){
		invio.set("ioType", 16);
		invio.set("ioTypeDesc", '检验入库');
	}else if (ioType==14){
		invio.set("ioType", 14);
		invio.set("ioTypeDesc", '领用入库');
	}
	invio.set("ioState", 1);
	invio.set("ioStateDesc", '草稿');
	invio.set("createTime", date);
	invio.set("ioDate", date);
	invio.set("warehouseUid",mWarehouseId);
	invio.set("creatorUid", loginUser.get("employeeUid"));
	invio.set("creator.employeeName", loginUser.get("employeeName"));
	
	var actionGetInvInBillNumber = view.id("actionGetInvInBillNumber");
	actionGetInvInBillNumber.execute(function(result){
		invio.set("invIOId", result);
	});
}

// @Bind #dataSetDropDownIoType.beforeExecute
!function(self, arg, dsTypesData) {
	dsTypesData.set("parameter", {
		typeName : "iotype"
	});
	dsTypesData.flush();
}

// @Bind #dataSetDropDownIoType.onValueSelect
!function(self, arg, dsTypesData, dsInvIO) {
	var invIO = dsInvIO.getData('#');
	var typesData = dsTypesData.getData('#');

	// dorado.MessageBox.alert();

	invIO.set("ioType", typesData.get("typeId"));

	invIO.set("ioTypeDesc.idDescription", typesData.get("idDescription"));

}

// @Bind #dataSetDropDownWarehouse.onValueSelect
!function(self, arg) {
	var dsWarehouse = view.id("dsWarehouse");

	var warehouse = dsWarehouse.getData("#");

	// var dsInvIODetail = view.id("dsInvIODetail");

	// var invIODetail = dsInvIODetail.getData("#");
	var dsInvIO = view.id("dsInvIO");

	var invIO = dsInvIO.getData("#");

	var invIODetails = dsInvIO.getData("#").get("details");

	var invIODetail = invIODetails.getLast();

	invIODetail.set("warebinUid", warehouse.get("warehouseId"));

	invIODetail.set("warebin.warehouse.warehouseName", warehouse
			.get("warehouseName"));

}

// @Bind #customDropDownType.beforeExecute
!function(self, arg) {
	var dataSet = view.id("dsClassInfo");
	dataSet.set("parameter", {
		parentClassUid : "Sort1_Sort1"
	});
	dataSet.flush();
}

// @Bind #classInfoTree.onDataRowClick
!function(self, arg) {
	var dropDown = dorado.widget.DropDown.findDropDown(self);
	var node = self.get("currentNode");
	// var curEntity = self.get("currentEntity");
	dropDown.close(node.get("data").get("className"));
}

/*// @Bind @dtInvIODetail.onInsert
!function(self, arg, dsInvIODetail, dsInvIO) {
	var invIO = dsInvIO.getData("#");
	var invIODetail = arg.entity;
	invIODetail.set("invIOUid", invIO.get("uuid"));
}*/

//@Bind #dgDetail.onContextMenu
!function(self, arg) {
	view.id("rightKeyMenu").show({
		position : {
			left : arg.event.pageX,
			top : arg.event.pageY
		}
	});

}

/*// @Bind #menuAdd.onClick
!function(self, arg,dsInvIODetail) {
	
	var invIODetail = dsInvIODetail.insert();
	dorado.MessageBox.alert(invIODetail); 
	
	invIODetail.set("ioQty", 1);

	invIODetail.set("batchnum","NA");

	invIODetail.set("sn","0");

	
}*/

//@Bind #manuDelete.onClick
!function(self, dsInvIODetail) {
	var invIODetail = dsInvIODetail.getData();
	
	if (invIODetail.toArray().length > 0){
		invIODetail.remove();
	}else {
		dorado.MessageBox.alert("没有可删除的数据...");
	}
}


var isFinishSetValue = true;
//@Bind #dtInvIODetail.onDataChange
!function(self, arg, dsInvIODetail) {
	var invIODetail = dsInvIODetail.getData("#");
	var ioQty = invIODetail.get("ioQty");
	var unitPrice = invIODetail.get("unitPrice");
	if(unitPrice == null){
		unitPrice=0;
	}
	var amount = invIODetail.get("amount");
	if(amount == null){
		amount=0;
	}
	if(arg.property=="ioQty"&&isFinishSetValue == true){
		isFinishSetValue = false;
		arg.entity.set("amount",arg.newValue*arg.entity.get("unitPrice"));
		isFinishSetValue = true
	}
	if(arg.property=="unitPrice"&&isFinishSetValue == true){
		isFinishSetValue = false;
		arg.entity.set("amount",arg.newValue*ioQty);
		isFinishSetValue = true
	}
	
	if(arg.property=="amount"&&isFinishSetValue == true){
		isFinishSetValue = false;
		arg.entity.set("unitPrice",arg.newValue/arg.entity.get("ioQty"));
		isFinishSetValue = true;
	}
}
